package com.yipei.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yipei.business.domain.MlRealName;
import com.yipei.business.mapper.MlRealNameMapper;
import com.yipei.business.service.IMlRealNameService;
import com.yipei.common.utils.DateUtils;
import com.yipei.common.utils.SecurityUtils;
import com.yipei.common.utils.bean.BeanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实名认证Service业务层处理
 *
 * @author yipei
 * @date 2024-12-21
 */
@Service
public class MlRealNameServiceImpl extends ServiceImpl<MlRealNameMapper, MlRealName> implements IMlRealNameService
{
    @Autowired
    private MlRealNameMapper mlRealNameMapper;


    /**
     * 查询实名认证
     *
     * @param id 实名认证主键
     * @return 实名认证
     */
    @Override
    public MlRealName selectMlRealNameById(Long id)
    {
        return mlRealNameMapper.selectMlRealNameById(id);
    }

    /**
     * 查询实名认证列表
     *
     * @param mlRealName 实名认证
     * @return 实名认证
     */
    @Override
    public List<MlRealName> selectMlRealNameList(MlRealName mlRealName)
    {
        return mlRealNameMapper.selectMlRealNameList(mlRealName);
    }

    /**
     * 新增实名认证
     *
     * @param mlRealName 实名认证
     * @return 结果
     */
    @Override
    public int insertMlRealName(MlRealName mlRealName) {
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        LambdaQueryWrapper<MlRealName> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MlRealName::getTdAcc,tdAcc);
        MlRealName one = this.getOne(lambdaQueryWrapper);
        if (ObjectUtils.isNotEmpty(one)&&one.getStatus().compareTo(3L)==0){
            one.setStatus(1L);
            one.setImage1(mlRealName.getImage1());
            one.setImage2(mlRealName.getImage2());
            one.setImage3(mlRealName.getImage3());
            one.setImage4(mlRealName.getImage4());
            one.setAddr(mlRealName.getAddr());
            one.setSfzHm(mlRealName.getSfzHm());
            one.setZsMz(mlRealName.getZsMz());
           return mlRealNameMapper.updateMlRealName(one);
        }else {
            mlRealName.setTdAcc(tdAcc);
            mlRealName.setCreateTime(DateUtils.getNowDate());
            mlRealName.setStatus(1L);
            return mlRealNameMapper.insertMlRealName(mlRealName);
        }

    }

    /**
     * 修改实名认证
     *
     * @param mlRealName 实名认证
     * @return 结果
     */
    @Override
    public int updateMlRealName(MlRealName mlRealName)
    {
        mlRealName.setUpdateTime(DateUtils.getNowDate());
        return mlRealNameMapper.updateMlRealName(mlRealName);
    }

    /**
     * 批量删除实名认证
     *
     * @param ids 需要删除的实名认证主键
     * @return 结果
     */
    @Override
    public int deleteMlRealNameByIds(Long[] ids)
    {
        return mlRealNameMapper.deleteMlRealNameByIds(ids);
    }

    /**
     * 删除实名认证信息
     *
     * @param id 实名认证主键
     * @return 结果
     */
    @Override
    public int deleteMlRealNameById(Long id)
    {
        return mlRealNameMapper.deleteMlRealNameById(id);
    }

    @Override
    public MlRealName getIsRealName() {
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        LambdaQueryWrapper<MlRealName> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MlRealName::getTdAcc,tdAcc);
        MlRealName one = this.getOne(lambdaQueryWrapper);
        return one;
    }
}
